package io.renren.modules.generator.controller;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.modules.generator.entity.ProjectEntity;
import io.renren.modules.generator.service.ProjectService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.ScoreEntity;
import io.renren.modules.generator.service.ScoreService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author author
 * @email author@gmail.com
 * @date 2021-07-01 11:12:11
 */
@RestController
@RequestMapping("generator/score")
public class ScoreController extends AbstractController {
    @Autowired
    private ScoreService scoreService;

    @Autowired
    private ProjectService projectService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:score:list")
    public R list(@RequestParam Map<String, Object> params){
        SysUserEntity sessionUser = getUser();
        if (!sessionUser.getUserId().equals(1L)) {
            params.put("expertId", sessionUser.getUserId());
        }
        PageUtils page = scoreService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:score:info")
    public R info(@PathVariable("id") Integer id){
		ScoreEntity score = scoreService.getById(id);

        return R.ok().put("score", score);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:score:save")
    public R save(@RequestBody ScoreEntity score){
		scoreService.save(score);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:score:update")
    public R update(@RequestBody ScoreEntity score){
		scoreService.updateById(score);
        ProjectEntity projectEntity = projectService.getById(score.getProjectId());
        List<ScoreEntity> scoreEntityList = scoreService.queryByProjectId(score.getProjectId());
        if (scoreEntityList.size() == 0) {
            return R.error();
        }
        // 几位专家是否评分完成
        boolean isFinish = true;
        Double totalScore = 0.0;
        Double avgScore = 0.0;
        DecimalFormat df = new DecimalFormat("######0.00");
        for (ScoreEntity scoreEntity : scoreEntityList) {
            if ("0".equals(scoreEntity.getMark())) {
                isFinish = false;
                break;
            }
            totalScore += Double.parseDouble(scoreEntity.getMark());
        }
        if (isFinish) {
            avgScore = totalScore / scoreEntityList.size();
            projectEntity.setMark(df.format(avgScore));
            projectEntity.setState(5);
            projectService.updateById(projectEntity);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:score:delete")
    public R delete(@RequestBody Integer[] ids){
		scoreService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
